
let search_input=document.querySelector('#search_input');
search_input.addEventListener('focus',function(){
    if(this.value==='音乐/视频/电台/用户') {
        this.value=null;
        this.style.color='black'
    }
    
})
search_input.addEventListener('blur',function(){
    if(this.value===''){
        this.value='音乐/视频/电台/用户';
        this.style.color='#666'
    }
    
})

//首界面
let usertx=document.querySelector('#usertx')
let oljm=document.querySelector('#otherlogin');
let closelogin4=document.querySelector('#closelogin4');
let logining=document.querySelector('#logining');
let oldl=document.querySelector('#oldl');
let olzc=document.querySelector('#olzc');
let loginboxnumber=document.querySelector('#loginboxnumber');
let zhuce=document.querySelector('#zhuce');
let agree=document.querySelector('#agree')
let agresult=0
agree.addEventListener('change',function(){
    if(this.checked) {
        agresult=1
    }
})

logining.addEventListener('click',function(){
    oljm.style.display='block'
})
closelogin4.addEventListener('click',function(){
    oljm.style.display='none'
})
//点击登录到短信登录
oldl.addEventListener('click',function(){
    if(!agresult){
        alert('请先勾选协议！')
        return
    }
    oljm.style.display='none';
    loginboxnumber.style.display='block';
})
//点击注册到注册
olzc.addEventListener('click',function(){
    if(!agresult){
        alert('请先勾选协议！')
        return
    }
    oljm.style.display='none';
    zhuce.style.display='block';
})


//点击X关闭短信验证码界面
let closelogin1=document.querySelector('#closeloginone')
closelogin1.addEventListener('click',function(){
    loginboxnumber.style.display='none';
})
//点击其他登录方式
let duanxinotherlogin=document.querySelector('#duanxinotherlogin')
duanxinotherlogin.addEventListener('click',function(){
    loginboxnumber.style.display='none';
    oljm.style.display='block' //需修改
})


//密码登录
let stlogin=document.querySelector('#stlogin')
let loginboxpw=document.querySelector('#loginboxpw')
stlogin.addEventListener('click',function() {
    loginboxnumber.style.display='none';
    loginboxpw.style.display='inline-block';
})
//点击X关闭密码登录界面
let closelogin2=document.querySelector('#closelogin2')
closelogin2.addEventListener('click',function(){
    loginboxpw.style.display='none';
})
//点击短信登录
let duanxin=document.querySelector('#duanxin')
duanxin.addEventListener('click',function(){
    loginboxnumber.style.display='inline-block';
    loginboxpw.style.display='none';
})
//点击其他登录方式
let mmolg=document.querySelector('#mmolg');
mmolg.addEventListener('click',function(){
    loginboxpw.style.display='none';
    oljm.style.display='block';
})

//注册点登录
let zcdl=document.querySelector('#zcdl');
zcdl.addEventListener('click',function(){
    zhuce.style.display='none';
    oljm.style.display='block';
})
//点击X关闭注册页面
let closelogin3=document.querySelector('#closelogin3')
closelogin3.addEventListener('click',function(){
    zhuce.style.display='none';
})


// 短信验证码
axios.defaults.baseURL='http://j48zhu.natappfree.cc'
const code=document.getElementById('code') 
const email=document.getElementById('email')
const getcodeBtn=document.getElementById('get_code')
let yzm=0
let btnclicked=0
let yzmsend=0
getcodeBtn.addEventListener('click',function(){
    const emailvalue=document.getElementById('email').value
    btnclicked=1
    if(!emailvalue) {
        alert('请输入手机号！');
        return;
    }
    // 模拟倒计时
    let count = 60;
    const timer = setInterval(() => {
        getcodeBtn.disabled = true;
        getcodeBtn.textContent = `${count}秒后重试`;
        count--;
        if (count < 0) {
            clearInterval(timer);
            getcodeBtn.disabled = false;
            getcodeBtn.textContent = '获取验证码';
        }
    }, 1000);
    

    axios.get('/user/captcha',{
        params: {
            email:emailvalue          
        }
        
    })
    .then(response => {
        if (response.data.code === 0) {
            alert('验证码已发送');
            console.log(response);
            yzm=response.data.data
            console.log(yzm);
            
            yzmsend=1;
        }
    })
})
const btnlogin=document.getElementById('btnlogin')
btnlogin.addEventListener('click',function(){
    const emailvalue=document.getElementById('email').value
    const codevalue=document.getElementById('code').value  
    if (!emailvalue) {
        alert('请输入手机号');
        return;
    }
    
    if (!codevalue) {
        alert('请输入验证码');
        return;
    }

    if(!btnclicked){
        alert('请点击获取验证码！');
        return;

    }

    if(!yzmsend) {
        alert('验证码还未发送！');
        return;
    }
    
    axios.post('/user/login',{
        params: {
            email:emailvalue,
            code:codevalue, //自己输入的验证码
            hcode:yzm
        }
    }).then(response=>{
        console.log(response);
        const codest=response.data.code
        const msg=response.data.msg
        if(code==0) {
            alert('登录成功！');
            loginboxnumber.style.display='none'
            logining.style.display='none'
            usertx.style.display='inline-block'
        }
        else if(codevalue!=yzm) {
            console.log(codevalue);
            console.log(yzm);
            
            alert('验证码输入错误！')
        }
        else {
                alert(response.data.msg || '登录失败');
        }
    })
})

//密码登录
const srnumber=document.getElementById('srnumber') //手机号
const srmm=document.getElementById('srmm')
const srbtnlogin=document.getElementById('srbtnlogin')
srbtnlogin.addEventListener('click',function(){
    const srnumbervalue=document.getElementById('srnumber').value
    const srmmvalue=document.getElementById('srmm').value
    if(!srnumbervalue) {
        alert('请输入手机号！');
        return;
    }
    if(!srmmvalue){
        alert('请输入密码！');
        return;
    }
    axios.post('/user/login', {
        email: srnumbervalue,
        password: srmmvalue
    })
    .then(response => {
        console.log(response);
        
        if (response.data.code === 0) {
            alert('登录成功');
            loginboxpw.style.display='none'
            logining.style.display='none'
            usertx.style.display='inline-block'
        } else {
            alert(response.data.msg || '登录失败');
        }
    })
    .catch(error => {
        console.error('请求失败:', error);
        alert('服务器错误');
    });
})

//注册
const inemail=document.getElementById('inemail');
const incode=document.getElementById('incode')
const ingetcode=document.getElementById('inget_code')
const inpwd=document.getElementById('inpwd')
const zhucebtn=document.getElementById('zhuce_btn')
let hcode
let zcyzmsend=0
let getyzm=0
ingetcode.addEventListener('click',function(){
    const inemailvalue=document.getElementById('inemail').value
    if(!inemailvalue){
        alert('请输入手机号！')
        return
    }
    getyzm=1
    // 模拟倒计时
    let count = 60;
    const timer = setInterval(() => {
        ingetcode.disabled = true;
        ingetcode.textContent = `${count}秒后重试`;
        count--;
        if (count < 0) {
            clearInterval(timer);
            ingetcode.disabled = false;
            ingetcode.textContent = '获取验证码';
        }
    }, 1000);
    
    axios.get('/user/captcha',{
        params: {
            email:inemailvalue          
        }
    }).then(response=>{
        console.log(response);
        hcode=response.data.data // 通过邮箱发送的正确验证码
        console.log(hcode); 
        if(response.data.code === 0) {
            alert('验证码已发送');
            zcyzmsend=1
        }
    })
    
})
zhucebtn.addEventListener('click',function(){
    const code=document.getElementById('incode').value
    const inemailvalue=document.getElementById('inemail').value
    const password=document.getElementById('inpwd').value
    if(!inemailvalue){
        alert('请输入手机号！')
        return
    }
    if(!code){
        alert('请输入验证码！')
        return
    }
    if(!getyzm){
        alert('请发送验证码！')
        return
    }
    if(!zcyzmsend){
        alert('验证码还未发送！')
        return
    }
    if(!password){
        alert('请输入密码！')
        return
    }
    var pattern = /^[\w_-]{6,16}$/;
    if(!pattern.test(password)){
        alert('密码必须为6~16位，由字母，数字，-和_构成')
        return
    }
    
    // console.log(code,password);
    axios.post('/user/register',{  
            "email":inemailvalue,
            "password":password,
            "code":code,
            "hcode":hcode
    }).then(response=>{
        if(response.data.code!=0){
            alert(response.data.msg)
            return
        }
        if(response.data.code==0){
            alert('注册成功！')
            zhuce.style.display='none'
            logining.style.display='none'
            usertx.style.display='inline-block'
            return
        }
        
    })
})

let list=document.querySelector('#list') 
let tuichu=document.querySelector('#tuichu')
usertx.addEventListener('click',function(){
    list.style.display='inline-block'
    console.log('success');
})
tuichu.addEventListener('click',function(){
    list.style.display='none'
    usertx.style.display='none'
    logining.style.display='block'
})
document.addEventListener('click', function(event) {
    if (!event.target.matches('#usertx') && !event.target.closest('#list')) {
        list.style.display = 'none';

    }
})

